package com.dzsw.comb;

import com.google.common.base.Joiner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CombMain2 {

    public static void main(String[] args) {
        List<String> list = Arrays.asList("a", "b", "c", "d", "e");
        List<String> l = combineTwo(list, false);
        System.out.println(l.size());
        System.out.println(Joiner.on(",").join(l));
    }

    public static String combine(List<String> list, int count) {
        List<String> resultList = new ArrayList<>();
        for (int offset = 0; offset < list.size(); offset++) {
            String firstWord = list.get(offset);
            for (int num = offset; num < list.size(); num++) {
                if (num + count > list.size()) {
                    continue;
                }
                StringBuilder sb = new StringBuilder(firstWord);
                for (int times = 0; times < count - 1; times++) {
                    int index = times + num;
                    String nextWord = list.get(index);
                    sb.append(nextWord);
                }
                resultList.add(sb.toString());
            }
        }
        System.out.println(resultList.size());
        return Joiner.on(",").join(resultList);
    }

    public static List<String> combineTwo(List<String> list, boolean repeat) {
        List<String> resultList = new ArrayList<>();
        for (int offset = 0; offset < list.size(); offset++) {
            String first = list.get(offset);
            int tmp = offset;
            if (!repeat) {
                tmp = offset + 1;
            }
            for (int i = tmp; i < list.size(); i++) {
                String second = list.get(i);
                resultList.add(first + second);
            }
        }
        return resultList;
    }
}
